<?php
class Exam_Form_Exam_Addquestion extends Engine_Form
{
  public $_error = array();

  public function init()
  {   
  	
  	$this
  	->setTitle('Add question')
  	->setDescription('Add new question for Exam !')
  	->setMethod('POST')
  	->setAction($_SERVER['REQUEST_URI'])
  	->setAttrib('class', 'add_question_form global_form')
  	->setAttrib('id','add_question_form');
  	
  	// category level 1
  	$this->addElement('Select', 'category', array(
  			'label' => 'Category',
  			'multiOptions' => array(
  					'all' => 'All Categories'
  			),
  	));
  	  		 
  	$this->addElement('TinyMce', 'content', array(
  			'label' => 'Question',
  			'id' => 'content',
  			'class' => 'content',
			'filters' => array(
        		'StringTrim',
      			),
  	));
  	
  	

//  	$params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
//  	
//  	$table = Engine_Api::_()->getItemTable('answer');
//  	$answers = $table->select()
//  	->where('question_id = '.$params['question_id'])
//  	->query()
//  	->fetchAll();
//  	
//  	if( !empty($answers) ){
//  		foreach ($answers as $answer){
//  			$this->addElement('Text',(string) $answer['answer_id'], array(
//  					'label' => 'Answer',
//  					'value' => $answer['content'],
//  					'id' => $answer['answer_id'],
//  					'class' => 'answer',
//  					'belongsTo' => 'answer'
//  			));
//  		};
//  	}
//  	else
//  	{ 	
  		$this->addElement('Text', "answer", array(
  				'label' => 'Answer',
  				'value' => 'Add answer ...',
  				'defaultvalue'=> 'Add answer ...',
  				'id' => 'answer_1',
  				'class' => 'answer',
  				'isArray' => true
  		));
//  	};
  	
  	$this->addElement('Text', 'answer_more', array(
  			'label' => 'Add More',
  			'value' => 'Add answer ...',
  			'id' => 'answer_more',
  	));
  	
    // Buttons
    $this->addElement('Button', 'submit', array(
      'label' => 'Add',
      'type' => 'submit',
      'ignore' => true,
      'decorators' => array('ViewHelper')
    ));

    $this->addElement('Cancel', 'cancel', array(
      'label' => 'cancel',
      'link' => true,
      'prependText' => ' or ',
      'href' => '',
      'onclick' => 'parent.Smoothbox.close();',
      'decorators' => array(
        'ViewHelper'
      )
    ));
    $this->addDisplayGroup(array('submit', 'cancel'), 'buttons');
    $button_group = $this->getDisplayGroup('buttons');
  }
  
  Public function saveValue()
  {
  	$params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
  	
  	$db = Engine_Api::_()->getItemTable('question')->getAdapter();
  	$table_question = Engine_Api::_()->getItemTable('question');
  	
  	$db->beginTransaction();
  	try {		 		
  		$question= $table_question->createRow();
  		$question->content = $params['content'];
  		$question->category_id = $params['category_lv1'];
  		$question->creation_date = date('Y-m-d H:i:s');
  		$question->modified_date   = date('Y-m-d H:i:s');
  		$question->save();
  		
  		$db->commit();
  	
  	}catch (Exception $e)
  	{
  		$db->rollBack();
  		throw $e;
  	}
  	
  	//add to Examdetail table
  	$table_detail = Engine_Api::_()->getDbTable('examdetails','exam');
  	
  	$detail = $table_detail->createRow();
  	$detail->exam_id = $params['exam_id'];
  	$detail->question_id = $question->question_id;
  	$detail->save();
  	
  	//Add answer to Answer table
  	$table_answer = Engine_Api::_()->getItemTable('answer');
  	
  	foreach( $params['answer'] as $key=>$value)
  	{
  		if( $this->getElement('answer_more')->getValue() != $value){
  			//create new info
  			$answer = $table_answer->createRow();
  			$answer->question_id = $question->question_id;
  			$answer->content = $value;
  			$answer->save();
  		};
  	};
  	
  	

  	
	return;
  }

}